package com.alex.bean;


import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

public class LoginForm extends ActionForm {
    private String username;
    private String password;
    private String login;

    public LoginForm() {
    }

    public LoginForm(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
//        System.out.println("reset");
        username = (String) request.getAttribute("username");
        password = "";
//        super.reset(mapping, request);
    }


    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
//        return super.validate(mapping, request);
        ActionErrors actionErrors = new ActionErrors();
        System.out.println("HttpServlet validate");
//        System.out.println(username);
//        System.out.println(new ActionMessage("test").toString());
        if (username==null || username.length()==0) {
//            System.out.println("username==null");
            actionErrors.add("username", new ActionMessage("login.username.null"));
//            actionErrors.add("username", new ActionMessage("login.username.null"));
        } else if (password==null || password.length()==0) {
            actionErrors.add("password", new ActionMessage("login.password.null"));
        }
//        System.out.println(actionErrors);
        return actionErrors;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "LoginForm{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
